<?php
namespace app\web\home;


use app\web\model\Follow as myModel;
use app\web\model\Technician;
use app\web\model\Shop;
use app\web\model\CouponType;
use app\web\model\Scheduling;
use app\web\model\TechnicianQualifications;
use app\web\model\User;
use app\web\model\Tags;
use app\web\model\Project;
use app\lib\model\Tool;
use think\Db;

/**
 * 首页\公共接口
 */
class Follow extends Base 
{
    /**
     * 技师关注/取消关注
     * [technicianFollow description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function technicianFollow($id)
    {
        $data = ['user_id'=>$this->uid,'follow_id'=>0,'technician_id'=>$id];
        $follow = myModel::where($data)->find();
        if($follow){
            $res = $follow->delete();
            Technician::where('id',$id)->setDec('follow_number');
            if($res) return show(200,'取消关注成功');
            return show(400,'取消关注失败');
        }else{
            $res = myModel::create($data);
            Technician::where('id',$id)->setInc('follow_number');
            if($res) return show(200,'关注成功');
            return show(400,'关注失败');
        }
    }
    /**
     * 项目关注/取消
     * [projectFollow description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function projectFollow($id){
        $data = ['user_id'=>$this->uid,'follow_id'=>1,'project_id'=>$id];
        $follow = myModel::where($data)->find();
        if($follow){
            $res = $follow->delete();
            if($res) return show(200,'取消收藏成功');
            return show(400,'取消收藏失败');
        }else{
            $res = myModel::create($data);
            if($res) return show(200,'收藏成功');
            return show(400,'收藏失败');
        }
    }
    public function myList($type = 0)
    {
        $user = User::get($this->uid);
        if($type == 0){
            $ids = myModel::where(['user_id'=>$this->uid,'follow_id'=>$type])->column('technician_id');
            if(!$ids) return show(200,'我的技师列表',[]);
            $product = Tool::getProduct($user->longitude,$user->latitude);
            $where = [['a.id','in',$ids]];
            $sql = Technician::alias('a')
                        ->where($where)
                        ->field('a.id,a.nickName,a.shop_mobile,a.score,a.order_number,a.shop_id,a.avatarUrl,a.avatar_similarity,a.real_status,a.traffic_id,a.is_recommend,a.product,a.longitude,a.latitude,a.tags_ids,a.qualifications_status,abs(a.product-'.($product/10000000).') as distance')
                        ->select(false);
            $list = Db::table('('.$sql.')')->alias('a')
                        ->leftjoin('shop b','b.id=a.shop_id')
                        ->leftjoin('traffic c','c.id=a.traffic_id')
                        ->field('a.*,ifnull(b.shop_name,"") as shop_name,ifnull(c.traffic_name,"") as traffic_name')
                        ->order('product')
                        ->paginate()->toArray();
            $data = $list['data'];
            $time = $this->request->time();
            $time_date = strtotime(date('Y-m-d'));
            foreach ($data as $key => $value) {
                $data[$key]['avatar_authentication'] = $value['avatar_similarity'] < 70 ? 0 : 1;   //头像是否认证
                $data[$key]['is_mobile'] = $data[$key]['shop_mobile'] ? 1 : 0;
                $coupon_type = CouponType::where(['coupon_type'=>2,'technician_id'=>$value['id'],'status'=>1])->field('money,full_money')->find();
                $data[$key]['coupon_money'] = $coupon_type['money'] ?? 0;
                $data[$key]['full_money'] = $coupon_type['full_money'] ?? 0;
                $data[$key]['distance'] = Tool::getDistance($user['longitude'],$user['latitude'],$value['longitude'],$value['latitude']);
                $data[$key]['tags'] = $value['tags_ids'] ? Tags::where('id','in',$value['tags_ids'])->column('tags_name') : [];
                $data[$key]['qualifications'] = TechnicianQualifications::alias('a')
                                                ->where(['a.technician_id'=>$value['id'],'a.status'=>1])
                                                ->leftjoin('job b','b.id=a.job_id')
                                                ->column('b.job_name');
                
                $scheduling = Scheduling::where([['technician_id','=',$value['id']],['start_time','<',$time],['start_time','>',$time-1800],['status','=',2]])->find();
                if($scheduling){  
                    $data[$key]['scheduling_status'] = 2;  //服务中
                }else{
                    $scheduling = Scheduling::where([['technician_id','=',$value['id']],['start_time','>',$time],['status','=',0]])->find();
                    $data[$key]['scheduling_status'] = $scheduling ? 0 : 1;
                }
                unset($data[$key]['product']);
                unset($data[$key]['longitude']);
                unset($data[$key]['latitude']);
                unset($data[$key]['shop_mobile']);
                unset($data[$key]['shop_id']);
                unset($data[$key]['avatar_similarity']);
                unset($data[$key]['tags_ids']);
                unset($data[$key]['traffic_id']);
            }
            $list['data'] = $data;
            return show(200,'技师列表',$list);      
        }else{
            $ids = myModel::where(['user_id'=>$this->uid,'follow_id'=>$type])->column('project_id');
            if(!$ids) return show(200,'我的项目列表',[]);
            $list = Project::alias('a')->where('a.id','in',$ids)
                            ->leftjoin('shop b','b.id=a.shop_id')
                            ->field('a.id,a.project_name,a.service_time,a.order_number,a.money,a.image,ifnull(b.shop_name,"") as shop_name')
                            ->order('a.order_number desc')
                            ->paginate()->each(function($v,$k){
                                $v['image'] = explode(',', $v['image'])[0];
                            });
            return show(200,'我的项目列表',$list);      
        }
        dd($ids);
    
    }
}